<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="20">
        <el-form :model="queryParams" ref="queryForm" :inline="true">
          <el-form-item label="商品编号" prop="name">
            <el-input v-model="queryParams.id" placeholder="请输入商品编号" clearable />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          </el-form-item>
        </el-form>
      </el-col>

    </el-row>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="商品编号" align="center" prop="id" width="300px" />
      <el-table-column label="图片" align="left" prop="imgUrl"  width="500px" >
        <template #default="scope">
          <div class="img-box">
            <el-image style="width: 100px; height: 50px" :src="scope.row.imgUrl" fit="cover" :preview-teleported="true">
            </el-image>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="商品名称" align="center" prop="name" width="300px" />
<!--      <el-table-column label="描述" align="center" prop="desr" width="180" />-->
<!--      <el-table-column label="分类" align="center" prop="cagegoryId" width="180" />-->
      <el-table-column label="库存" align="center" prop="storeNum" width="400px" />
      <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link size="small" type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.size"
      @pagination="getProductLists" />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="dialogTitleText" v-model="open" width="600px" append-to-body>
      <!-- <el-form v-if="dialogTitleText == '编辑商品' ? false : true" ref="addFormRef" :model="form" :rules="rules"
        label-width="120px">
        <el-form-item label="商品名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品描述:" prop="desr">
          <el-input v-model="form.desr" placeholder="请输入商品描述"></el-input>
        </el-form-item>
        <el-form-item label="商品零售价:" prop="price">
          <el-input v-model="form.price" placeholder="请输入商品零售价"></el-input>
        </el-form-item>
        <el-form-item label="促销价:" prop="cuxiaoPrice">
          <el-input v-model="form.cuxiaoPrice" placeholder="请输入商品促销价"></el-input>
        </el-form-item>
        <el-form-item label="会员价:" prop="vipPrice">
          <el-input v-model="form.vipPrice" placeholder="请输入会员价"></el-input>
        </el-form-item>
        <el-form-item label="商品分类:" prop="cagegoryId">
          <el-select v-model="form.cagegoryId" filterable clearable placeholder="请选择类型">
            <el-option v-for="(item) in moreArr" :key="item.value" :label="item.label" :value="item.value">

            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传图片:" prop="remark">
          <div class="flex-box">
            <UpFile ref="upFileRef" :file-list="fileList" multiple :limit="1" :size="1"
              accept=".jpg,.png,.gif,.jpeg,.webp"></UpFile>
          </div>
        </el-form-item>
      </el-form> -->
      <el-form  ref="editFormRef" :model="editForm" :rules="rules" label-width="120px">
        <el-form-item label="商品编号:" prop="id" >
          <el-input disabled v-model="editForm.id" placeholder="商品编号"  />
        </el-form-item>
        <el-form-item label="商品名称：" prop="name">
          <el-input v-model="editForm.name" placeholder="商品名称" disabled ></el-input>
        </el-form-item>
        <el-form-item label="商品库存:" prop="storeNum">
          <el-input v-model="editForm.storeNum" placeholder="请输入库存"  ></el-input>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer" v-loading="loading">
          <el-button @click="open = false">取 消</el-button>
          <el-button type="primary" @click="submitForm()">保 存</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import UpFile from "@/components/upFile/index.vue";
import { addFile, upFileReq, getImg, delFile } from "@/api/components/index.js";
import { getProductList, addProduct,editProduct,delProduct,editProductStoreNum } from "@/api/product/index.js"
import { reactive } from "vue";

const { proxy } = getCurrentInstance();
const list = ref([]);
const open = ref(false);
const loading = ref(false);
const fileList = ref([]);
const upFileRef = ref(null);
const dialogTitleText = ref('')
const total = ref(0);


let moreArr = reactive([
  { value: 1, label: '男士外套' },
  { value: 2, label: '体育用品' },
]);

let fileId;
const data = reactive({
  form: {},
  editForm: {
    name: '',
    desr: '',
    price: '',
    cuxiaoPrice: '',
    vipPrice: '',
    cagegoryId: '',
    storeNum: '',
    imgUrl:'',
    id: ''
  },
  queryParams: {
    page: 1,
    size: 10,
  },
});
const { editForm, form, queryParams } = toRefs(data);
function handleAdd() {
  open.value = true;
  dialogTitleText.value = '新增商品'
  fileId = undefined;
  nextTick(() => {
    upFileRef.value.clearFiles();
  })
}
async function handleUpdate(row) {
  open.value = true;
  fileId = row.id;
  nextTick(() => {
    fileList.value = imgEvent(row);
  })
  dialogTitleText.value = '编辑库存信息'
  editForm.value.name = row.name
  editForm.value.id = row.id
  editForm.value.storeNum = row.storeNum
  // editForm.value.imgUrl = row.imgUrl


}
async function submitForm() {

    try {
      loading.value = true;
      const data = { ...editForm.value};
      !form.value.id && await editProductStoreNum({ ...data });
      proxy.$modal.msgSuccess( "修改成功");
      open.value = false;
      getProductLists();
    } finally {
      loading.value = false;
    }

  

}
async function handleDelete(row) {
  await proxy.$modal.confirm("确认删除当前商品数据吗？");
  await delProduct({ id: row.id });
  proxy.$modal.msgSuccess("删除成功");
  getProductLists();

}
async function getProductLists() {
  try {
    loading.value = true;
    const { data, total: totalAll } = await getProductList(queryParams.value);
    total.value = totalAll;
    list.value = data;
    list.value.forEach(ele => {
      ele.cagegoryId = ele.cagegoryId == '1' ? '男士外套' : '体育用品'
      ele.price = ele.price + '元'
      ele.vipPrice = ele.vipPrice + '元'
      ele.cuxiaoPrice = ele.cuxiaoPrice + '元'

    })
  } finally {
    loading.value = false;
  }
}
function handleQuery() {
  queryParams.value.page = 1;
  getProductLists();
}
function imgEvent(row) {
  try {
    return JSON.parse(row.val)
  } catch (e) {
    return []
  }
}
// getImgApi();
getProductLists()
</script>

<style scoped lang="scss">
.flex-box {
  text-align: center;
}

.img-box {
  display: flex;
  align-items: center;
  flex-wrap: nowrap;
  overflow: auto;

  >div {
    margin: 0 5px;
  }
}
</style>